<?php

class Default_ReviewsController extends Zend_Controller_Action
{
    protected $dbMapper, $dbTable;

    public function init()
    {
        //@todo Убрать данный костыль автозагрузкой
        require_once(APPLICATION_PATH . '/modules/admin/models/DbMapper/Reviews.php');
        require_once(APPLICATION_PATH . '/modules/admin/models/DbTable/Reviews.php');

        $this->dbMapper = new Admin_Model_DbMapper_Reviews();
        $this->dbTable = new Admin_Model_DbTable_Reviews();
    }

    protected function prepareData($data)
    {
        $validData = array();
        if($this->_request->isPost()) {
            try {
                if($this->dbMapper->savePhoto()) {
                  $validData['photo_url'] = $this->dbMapper->getPhotoUrl();
                  $validData['small_photo_url'] = $this->dbMapper->getSmallPhotoUrl();
                }
            } catch (Exception $e) {
                $this->view->errorMessage = 'Возникла ошибка: ' . $e->getMessage();
            }

            foreach($data as $index => $value){
               if(in_array($index, $this->dbMapper->getColNames())) {
                   $validData[$index] = strip_tags($value);
               }
            }
        }
        return $validData;
    }

    public function addAction()
    {
        $this->_helper->layout->disableLayout();

        if($this->_request->isPost()) {
            $data = $this->_request->getPost();
            $validData = $this->prepareData($data);
            $this->view->isInsert = (bool) $this->dbMapper->insert($validData);
        }
    }

    public function indexAction()
    {

        $this->forward('view');

    }


    public function viewAction()
    {
        $this->view->reviews = $this->dbMapper->getList();
    }
}

